import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import { getHots } from "../../api";

// 创建异步action
const getList = createAsyncThunk("getList", async (type) => {
    const res = await getHots(type);
    return res.result.slice(0, 4);
});

const listSlice = createSlice({
    name: "list",
    initialState: {
        loading: false,
        list: [],
    },
    extraReducers: (builder) => {
        builder.addCase(getList.pending, (state, action) => {
            console.log(action);
            state.loading = true;
        });
        builder.addCase(getList.fulfilled, (state, action) => {
            state.loading = false;
            console.log(action);
            state.list = action.payload;
        });
        builder.addCase(getList.rejected, (state) => {
            state.loading = false;
        });
    },
});

export { getList };

export default listSlice.reducer;
